package com.codeoss.module.stock.pagebean;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.view.pagebean.mobile.MDetailPageBean;
import com.codeoss.framework.view.service.IVariableService;
import com.codeoss.module.stock.service.IStockStorageService;

@Service("mStockInventoryLineDetailPageBean")
public class MStockInventoryLineDetailPageBean  extends MDetailPageBean {
	@Autowired
	private IVariableService variableService;
	@Autowired
	private IStockStorageService stockStorageService;
	
	public void onProductChangeHandler() {
		// TODO Auto-generated method stub
		calculateQtyBook();
	}

	
	public void onLocatorChangeHandler() {
		// TODO Auto-generated method stub
		calculateQtyBook();
	}

	private void calculateQtyBook() {
		YunContext yunContext = YunContext.getCurrentContext();
		Long productId = variableService.getVariableValue(yunContext, "stock_inventory_line_product_product_id");
		Long productAttributeSetInstanceId = variableService.getVariableValue(yunContext, "stock_inventory_line_product_attribute_set_instance_id");
		Long stockLocatorId = variableService.getVariableValue(yunContext, "stock_inventory_line_stock_locator_id");
		if(productId!=null && stockLocatorId!=null) {
			BigDecimal qtyBook=stockStorageService.getQtyBook(stockLocatorId, productId, productAttributeSetInstanceId);
			variableService.setVariableValue(yunContext, "stock_inventory_line_qty_book", qtyBook);
		}
	}
}
